/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pneuservis.backend.dao.impl;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pneuservis.backend.dao.CustomerDAOLocal;
import pneuservis.backend.entities.Customer;

/**
 *
 * @author Admin
 */
@Stateless
public class CustomerDAO implements CustomerDAOLocal {

    @PersistenceContext
    EntityManager entityManager;

    @Override
    public void createCustomer(Customer customer) {
        if (customer == null || customer.getId() != null || customer.getAddress() == null
                || customer.getName() == null || customer.getTelephone() == null) {
            throw new IllegalArgumentException("Create customer called with wrong param.");
        }

        entityManager.persist(customer);
    }

    @Override
    public Customer getCustomer(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("getCustomer called with null param.");
        }

        Customer result = entityManager.find(Customer.class, id);
        if (result == null) {
            return null;
        }
        entityManager.detach(result);
        return result;
    }

    @Override
    public void updateCustomer(Customer customer) {
        if (customer == null || customer.getId() == null || customer.getAddress() == null
                || customer.getName() == null || customer.getTelephone() == null) {
            throw new IllegalArgumentException("Update customer called with wrong param");
        }

        Customer find = entityManager.find(Customer.class, customer.getId());
        if (find == null) {
            throw new IllegalArgumentException(customer + " is not in db, but attempt to update.");
        }
        if (!customer.equals(find)) {
            throw new IllegalArgumentException("Very fucked up.");
        }

        entityManager.merge(customer);
    }

    @Override
    public void removeCustomer(Customer customer) {
        if (customer == null || customer.getId() == null) {
            throw new IllegalArgumentException("Remove customer called with wrong param");
        }
        Customer result = entityManager.find(Customer.class, customer.getId());
        entityManager.remove(result);
    }

    @Override
    public List<Customer> getAllCustomers() {
        List<Customer> result;

        Query query = entityManager.createQuery("SELECT c FROM Customer c ORDER BY c.id");
        result = query.getResultList();
        for (Customer c : result) {
            entityManager.detach(c);
        }
        return result;
    }

    @Override
    public List<Customer> findCustomerByName(String name) {

        List<Customer> result;
        Query query = entityManager.createQuery("SELECT c FROM Customer c WHERE c.name = :name ORDER BY c.id");
        query.setParameter("name", name);
        result = query.getResultList();
        for (Customer c : result) {
            entityManager.detach(c);
        }
        return result;
    }

    @Override
    public Customer getCustomerByUsername(String username) {
        if (username == null) {
            throw new IllegalArgumentException("getCustomerByUsername called with null param.");
        }
        Customer result;
        
        Query query = entityManager.createQuery("SELECT c FROM Customer c WHERE c.username = :username");
        query.setParameter("username", username);
//        try {
            result = (Customer) query.getSingleResult();
            return result;
//        } catch (NoResultException ex) {
//            return null;
//        }
    }
}
